package com.example.dryyzx.controller;

import com.example.dryyzx.dao.CustomerMapper;
import com.example.dryyzx.dao.OutgoingRecordMapper;
import com.example.dryyzx.encapsulation.ApprovalGoOutRecord;
import com.example.dryyzx.encapsulation.ApprovalGoOutRecordReturn;
import com.example.dryyzx.encapsulation.GoOutList;
import com.example.dryyzx.encapsulation.GoOutListReturn;
import com.example.dryyzx.entity.CompareDate;
import com.example.dryyzx.entity.Customer;
import com.example.dryyzx.entity.OutgoingRecord;
import com.example.dryyzx.entity.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Objects;

@Controller
public class OutgoingRecordController {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    private OutgoingRecordMapper outgoingRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;


    //外出登记
    @RequestMapping("go_out_page/new_go_out_record")
    public @ResponseBody String addOutgoingApplication(String name,String identify_number,String reason,String go_out_date,
                                                       String go_out_time,String due_date,String due_time,String accompany,
                                                       String relationship,String tel,String remark) throws ParseException {
        OutgoingRecord outgoingRecord = new OutgoingRecord();
        if(identify_number.length() != 18){
            return new JsonResult(false,"身份证号格式输入错误").toString();
        }
        Customer customer = customerMapper.selectByIdNumber(identify_number);
        if(customer == null){
            ArrayList<Customer> customers = customerMapper.selectByName(name);
            if(customers.size() == 0) {
                return new JsonResult(false, "没有该名字和身份证号的客人").toString();
            }else {
                return new JsonResult(false,"没有身份证号匹配的客人").toString();
            }
        }
        outgoingRecord.setCustomerId(customer.getId());
        outgoingRecord.setCompanion(accompany);
        outgoingRecord.setCompanionRelation(relationship);
        outgoingRecord.setCompanionPhone(tel);
        outgoingRecord.setReason(reason);
        outgoingRecord.setOutTime(sdf.parse(go_out_date+" "+go_out_time));
        outgoingRecord.setReturnTimeForecast(sdf.parse(due_date+" "+due_time));
        outgoingRecord.setRemarks(remark);
        if(customer.getName().equals(name)){
            if(customer.getState().equals("已外出")){
                return new JsonResult(false,"该客人已经外出").toString();
            }else {
                customer.setState("已外出");
                outgoingRecordMapper.insertSelective(outgoingRecord);
                customerMapper.updateByPrimaryKeySelective(customer);
                return new JsonResult(true, null).toString();
            }
        }else {
            return new JsonResult(false,"老人名字和身份证号不匹配").toString();
        }
    }


    //外出回来登记
    @RequestMapping(value ="go_out_page/return",method = RequestMethod.POST)
    public @ResponseBody String personReturn(String return_date,String return_time,String id) throws ParseException {
        OutgoingRecord outgoingRecord = outgoingRecordMapper.selectByPrimaryKey(Integer.parseInt(id));
        if(outgoingRecord != null) {
            outgoingRecord.setReturnTimeActual(sdf.parse(return_date + " " + return_time));
            Customer customer = customerMapper.selectByPrimaryKey1(outgoingRecord.getCustomerId());
            customer.setState("入住中");
            customerMapper.updateByPrimaryKeySelective(customer);
            outgoingRecordMapper.updateByPrimaryKeySelective(outgoingRecord);
            return new JsonResult(true, null).toString();
        }else {
            return new JsonResult(false,"出院申请表获取错误").toString();
        }
    }

    //修改外出登记
    @RequestMapping(value ="go_out_page/change_go_out_record",method = RequestMethod.POST)
    public @ResponseBody String changeGoOutRecord(String reason,String go_out_date,
                                                  String go_out_time,String due_date,String due_time,String accompany,
                                                  String relationship,String tel,String remark,String id) throws ParseException {

        OutgoingRecord outgoingRecord = outgoingRecordMapper.selectByPrimaryKey(Integer.valueOf(id));
        outgoingRecord.setOutTime(sdf.parse(go_out_date+" "+go_out_time));
        outgoingRecord.setReturnTimeForecast(sdf.parse(due_date+" "+due_time));
        outgoingRecord.setCompanion(accompany);
        outgoingRecord.setCompanionRelation(relationship);
        outgoingRecord.setCompanionPhone(tel);
        outgoingRecord.setReason(reason);
        outgoingRecord.setRemarks(remark);
        outgoingRecordMapper.updateByPrimaryKeySelective(outgoingRecord);
        return new JsonResult(true, null).toString();
    }


    //删除外出登记
    @RequestMapping(value ="go_out_page/delete_record",method = RequestMethod.POST)
    public @ResponseBody String deleteRecord(String id) throws ParseException {
        OutgoingRecord outgoingRecord = outgoingRecordMapper.selectByPrimaryKey(Integer.valueOf(id));
        if(outgoingRecord != null) {
            Customer customer = customerMapper.selectByPrimaryKey1(outgoingRecord.getCustomerId());
            customer.setState("入住中");
            customerMapper.updateByPrimaryKeySelective(customer);
            outgoingRecordMapper.deleteByPrimaryKey(Integer.valueOf(id));
            return new JsonResult(true,null).toString();
        }else {
            return new JsonResult(false,"没有这条外出记录").toString();
        }
    }



    //搜索外出登记
    @RequestMapping(value ="go_out_page/search_go_out_record",method = RequestMethod.POST)
    public @ResponseBody String searchRecord(String search_key,String page) throws ParseException {
        int max = 10;
        if(Integer.parseInt(page)<=0){
            return new JsonResult(false,"页数输入错误").toString();
        }
        //如果是null 输出所有外出记录的信息
        if(search_key == null|| search_key.equals("")){
            ArrayList<OutgoingRecord> outgoingRecords = outgoingRecordMapper.selectAll();
            if(outgoingRecords.size() == 0){
                return new JsonResult(false,"表内没有外出记录").toString();
            }else {
                ArrayList<GoOutList> goOutLists = new ArrayList<GoOutList>();
                for (int i=0;i<outgoingRecords.size();i++) {
                    OutgoingRecord addedOutGoingRecord = outgoingRecords.get(i);
                    Customer customer = customerMapper.selectByPrimaryKey1(addedOutGoingRecord.getCustomerId());

                    String[] goOutTimeParts = sdf.format(addedOutGoingRecord.getOutTime()).split(" ");
                    String[] forecastTimeParts = sdf.format(addedOutGoingRecord.getReturnTimeForecast()).split(" ");

                    if (addedOutGoingRecord.getReturnTimeActual() == null && addedOutGoingRecord.getApprovalTime() == null) {
                        GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(), customer.getName(), customer.getSex(),
                                customer.getAge(), customer.getArchivesid(), addedOutGoingRecord.getReason(), goOutTimeParts[0], goOutTimeParts[1],
                                forecastTimeParts[0], forecastTimeParts[1], "", "", addedOutGoingRecord.getCompanion(),
                                addedOutGoingRecord.getCompanionPhone(), addedOutGoingRecord.getApprovalStatus(), addedOutGoingRecord.getApprover(),
                                "", "", addedOutGoingRecord.getRemarks());
                        //将上面的go_out_list类添加进GoOutLists
                        goOutLists.add(goOutList);
                    } else if (addedOutGoingRecord.getReturnTimeActual() == null) {
                        String[] approvalTimeParts = sdf.format(addedOutGoingRecord.getApprovalTime()).split(" ");
                        GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(), customer.getName(), customer.getSex(),
                                customer.getAge(), customer.getArchivesid(), addedOutGoingRecord.getReason(), goOutTimeParts[0], goOutTimeParts[1],
                                forecastTimeParts[0], forecastTimeParts[1], "", "", addedOutGoingRecord.getCompanion(),
                                addedOutGoingRecord.getCompanionPhone(), addedOutGoingRecord.getApprovalStatus(), addedOutGoingRecord.getApprover(),
                                approvalTimeParts[0], approvalTimeParts[1], addedOutGoingRecord.getRemarks());
                        //将上面的go_out_list类添加进GoOutLists
                        goOutLists.add(goOutList);
                    } else if (addedOutGoingRecord.getApprovalTime() == null) {
                        String[] actualTimeParts = sdf.format(addedOutGoingRecord.getReturnTimeActual()).split(" ");
                        GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(), customer.getName(), customer.getSex(),
                                customer.getAge(), customer.getArchivesid(), addedOutGoingRecord.getReason(), goOutTimeParts[0], goOutTimeParts[1],
                                forecastTimeParts[0], forecastTimeParts[1], actualTimeParts[0], actualTimeParts[1], addedOutGoingRecord.getCompanion(),
                                addedOutGoingRecord.getCompanionPhone(), addedOutGoingRecord.getApprovalStatus(), addedOutGoingRecord.getApprover(),
                                "", "", addedOutGoingRecord.getRemarks());
                        //将上面的go_out_list类添加进GoOutLists
                        goOutLists.add(goOutList);
                    } else {
                        String[] actualTimeParts = sdf.format(addedOutGoingRecord.getReturnTimeActual()).split(" ");
                        String[] approvalTimeParts = sdf.format(addedOutGoingRecord.getApprovalTime()).split(" ");
                        GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(), customer.getName(), customer.getSex(),
                                customer.getAge(), customer.getArchivesid(), addedOutGoingRecord.getReason(), goOutTimeParts[0], goOutTimeParts[1],
                                forecastTimeParts[0], forecastTimeParts[1], actualTimeParts[0], actualTimeParts[1], addedOutGoingRecord.getCompanion(),
                                addedOutGoingRecord.getCompanionPhone(), addedOutGoingRecord.getApprovalStatus(), addedOutGoingRecord.getApprover(),
                                approvalTimeParts[0], approvalTimeParts[1], addedOutGoingRecord.getRemarks());
                        //将上面的go_out_list类添加进GoOutLists
                        goOutLists.add(goOutList);
                    }
                }
                //现在所有记录都在goOutLists这个变量里面了
                GoOutListReturn goOutListReturn = new GoOutListReturn();
                int total_page ;
                int offset = outgoingRecords.size()%max;
                if(offset == 0) {
                    total_page = goOutLists.size() / max ;//总页数
                }else {
                    total_page = goOutLists.size() / max + 1;
                }
                if(Integer.parseInt(page)>total_page){
                    return new JsonResult(false,"查询页数超过总页数").toString();
                }else {
                    //删除其他元素，使得outGoingRecords里面只有所需要的那一页的元素
                    //删除这一页前面的
                    for (int i = 0; i < (Integer.parseInt(page) - 1) * max; i++) {
                        goOutLists.remove(0);
                    }
                    //取出这一页的元素

                    if(Integer.parseInt(page)<total_page){
                        goOutListReturn.setTotal_page(total_page);
                        for (int i=0;i<max;i++){
                            goOutListReturn.setTotal_page(total_page);
                            goOutListReturn.addData(goOutLists.get(i));
                        }
                    }else {
                        for (int i=0;i<offset;i++){
                            goOutListReturn.setTotal_page(total_page);
                            goOutListReturn.addData(goOutLists.get(i));
                        }
                    }
                }
                return new JsonResult(true,goOutListReturn).toString();

            }
        }
        //如果是人名
        if(search_key.charAt(0) != '2'){
            ArrayList<Customer> customers = customerMapper.selectByHazyName(search_key);
            if(customers.size() == 0){
                return new JsonResult(false,"没有搜索到该名字客人").toString();
            }
            //有这个名字客人的外出记录
            ArrayList<OutgoingRecord> outgoingRecords = new ArrayList<OutgoingRecord>();
            ArrayList<GoOutList> goOutLists = new ArrayList<>();
            GoOutListReturn goOutListReturn = new GoOutListReturn();
            for (int i = 0;i< customers.size();i++){
                Customer customer = customers.get(i);
                OutgoingRecord addedOutGoingRecord = outgoingRecordMapper.selectByCustomerId(customer.getId());
                if(addedOutGoingRecord != null){
                    outgoingRecords.add(addedOutGoingRecord);
                }
            }
            //将outGoingRecords里面的每个record按照外出时间排序
            CompareDate cd = new CompareDate();
            Collections.sort(outgoingRecords,cd);

            if(outgoingRecords.size() == 0){
                return new JsonResult(false,"没有该人名的外出记录").toString();
            }

            //将每个record信息填入goOutList类中,并加入goOutLists里
            for(int i = 0;i< outgoingRecords.size();i++){
                OutgoingRecord addedOutGoingRecord = outgoingRecords.get(i);
                Customer customer = customerMapper.selectByPrimaryKey1(addedOutGoingRecord.getCustomerId());

                String[] goOutTimeParts = sdf.format(addedOutGoingRecord.getOutTime()).split(" ");
                String[] forecastTimeParts = sdf.format(addedOutGoingRecord.getReturnTimeForecast()).split(" ");

                if(addedOutGoingRecord.getReturnTimeActual()==null && addedOutGoingRecord.getApprovalTime()==null){
                    GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(),customer.getName(),customer.getSex(),
                            customer.getAge(),customer.getArchivesid(),addedOutGoingRecord.getReason(),goOutTimeParts[0],goOutTimeParts[1],
                            forecastTimeParts[0],forecastTimeParts[1],"","",addedOutGoingRecord.getCompanion(),
                            addedOutGoingRecord.getCompanionPhone(),addedOutGoingRecord.getApprovalStatus(),addedOutGoingRecord.getApprover(),
                            "","", addedOutGoingRecord.getRemarks());
                    //将上面的go_out_list类添加进GoOutLists
                    goOutLists.add(goOutList);
                } else if (addedOutGoingRecord.getReturnTimeActual()==null) {
                    String[] approvalTimeParts = sdf.format(addedOutGoingRecord.getApprovalTime()).split(" ");
                    GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(),customer.getName(),customer.getSex(),
                            customer.getAge(),customer.getArchivesid(),addedOutGoingRecord.getReason(),goOutTimeParts[0],goOutTimeParts[1],
                            forecastTimeParts[0],forecastTimeParts[1],"","",addedOutGoingRecord.getCompanion(),
                            addedOutGoingRecord.getCompanionPhone(),addedOutGoingRecord.getApprovalStatus(),addedOutGoingRecord.getApprover(),
                            approvalTimeParts[0],approvalTimeParts[1], addedOutGoingRecord.getRemarks());
                    //将上面的go_out_list类添加进GoOutLists
                    goOutLists.add(goOutList);
                } else if (addedOutGoingRecord.getApprovalTime()==null) {
                    String[] actualTimeParts = sdf.format(addedOutGoingRecord.getReturnTimeActual()).split(" ");
                    GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(),customer.getName(),customer.getSex(),
                            customer.getAge(),customer.getArchivesid(),addedOutGoingRecord.getReason(),goOutTimeParts[0],goOutTimeParts[1],
                            forecastTimeParts[0],forecastTimeParts[1],actualTimeParts[0],actualTimeParts[1],addedOutGoingRecord.getCompanion(),
                            addedOutGoingRecord.getCompanionPhone(),addedOutGoingRecord.getApprovalStatus(),addedOutGoingRecord.getApprover(),
                            "","", addedOutGoingRecord.getRemarks());
                    //将上面的go_out_list类添加进GoOutLists
                    goOutLists.add(goOutList);
                }else {
                    String[] actualTimeParts = sdf.format(addedOutGoingRecord.getReturnTimeActual()).split(" ");
                    String[] approvalTimeParts = sdf.format(addedOutGoingRecord.getApprovalTime()).split(" ");
                    GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(), customer.getName(), customer.getSex(),
                            customer.getAge(), customer.getArchivesid(), addedOutGoingRecord.getReason(), goOutTimeParts[0], goOutTimeParts[1],
                            forecastTimeParts[0], forecastTimeParts[1], actualTimeParts[0], actualTimeParts[1], addedOutGoingRecord.getCompanion(),
                            addedOutGoingRecord.getCompanionPhone(), addedOutGoingRecord.getApprovalStatus(), addedOutGoingRecord.getApprover(),
                            approvalTimeParts[0], approvalTimeParts[1], addedOutGoingRecord.getRemarks());
                    //将上面的go_out_list类添加进GoOutLists
                    goOutLists.add(goOutList);
                }


            }
            int total_page ;
            int offset = outgoingRecords.size()%max;
            if(offset == 0) {
                total_page = goOutLists.size() / max ;//总页数
            }else {
                total_page = goOutLists.size() / max + 1;
            }
            if(Integer.parseInt(page)>total_page){
                return new JsonResult(false,"查询页数超过总页数").toString();
            }else {
                //删除其他元素，使得outGoingRecords里面只有所需要的那一页的元素
                //删除这一页前面的
                for (int i = 0; i < (Integer.parseInt(page) - 1) * max; i++) {
                    goOutLists.remove(0);
                }
                //取出这一页的元素

                if(Integer.parseInt(page)<total_page){
                    goOutListReturn.setTotal_page(total_page);
                    for (int i=0;i<max;i++){
                        goOutListReturn.setTotal_page(total_page);
                        goOutListReturn.addData(goOutLists.get(i));
                    }
                }else {
                    for (int i=0;i<(offset+9)%10+1;i++){
                        goOutListReturn.setTotal_page(total_page);
                        goOutListReturn.addData(goOutLists.get(i));
                    }
                }
            }
            return new JsonResult(true,goOutListReturn).toString();
        }else {

            //如果是时间
            //Date a=date.parse(search_key);
            ArrayList<OutgoingRecord> outgoingRecords = outgoingRecordMapper.selectByOutTimeDate(search_key);
            GoOutListReturn goOutListReturn = new GoOutListReturn();
            if(outgoingRecords.size() == 0){
                return new JsonResult(false,"这个日期没有外出记录").toString();
            }
            ArrayList<GoOutList> goOutLists = new ArrayList<>();
            for (int i=0;i<outgoingRecords.size();i++){
                OutgoingRecord addedOutGoingRecord = outgoingRecords.get(i);
                Customer customer = customerMapper.selectByPrimaryKey1(addedOutGoingRecord.getCustomerId());

                String[] goOutTimeParts = sdf.format(addedOutGoingRecord.getOutTime()).split(" ");
                String[] forecastTimeParts = sdf.format(addedOutGoingRecord.getReturnTimeForecast()).split(" ");

                if(addedOutGoingRecord.getReturnTimeActual()==null && addedOutGoingRecord.getApprovalTime()==null){
                    GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(),customer.getName(),customer.getSex(),
                            customer.getAge(),customer.getArchivesid(),addedOutGoingRecord.getReason(),goOutTimeParts[0],goOutTimeParts[1],
                            forecastTimeParts[0],forecastTimeParts[1],"","",addedOutGoingRecord.getCompanion(),
                            addedOutGoingRecord.getCompanionPhone(),addedOutGoingRecord.getApprovalStatus(),addedOutGoingRecord.getApprover(),
                            "","", addedOutGoingRecord.getRemarks());
                    //将上面的go_out_list类添加进GoOutLists
                    goOutLists.add(goOutList);
                } else if (addedOutGoingRecord.getReturnTimeActual()==null) {
                    String[] approvalTimeParts = sdf.format(addedOutGoingRecord.getApprovalTime()).split(" ");
                    GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(),customer.getName(),customer.getSex(),
                            customer.getAge(),customer.getArchivesid(),addedOutGoingRecord.getReason(),goOutTimeParts[0],goOutTimeParts[1],
                            forecastTimeParts[0],forecastTimeParts[1],"","",addedOutGoingRecord.getCompanion(),
                            addedOutGoingRecord.getCompanionPhone(),addedOutGoingRecord.getApprovalStatus(),addedOutGoingRecord.getApprover(),
                            approvalTimeParts[0],approvalTimeParts[1], addedOutGoingRecord.getRemarks());
                    //将上面的go_out_list类添加进GoOutLists
                    goOutLists.add(goOutList);
                } else if (addedOutGoingRecord.getApprovalTime()==null) {
                    String[] actualTimeParts = sdf.format(addedOutGoingRecord.getReturnTimeActual()).split(" ");
                    GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(),customer.getName(),customer.getSex(),
                            customer.getAge(),customer.getArchivesid(),addedOutGoingRecord.getReason(),goOutTimeParts[0],goOutTimeParts[1],
                            forecastTimeParts[0],forecastTimeParts[1],actualTimeParts[0],actualTimeParts[1],addedOutGoingRecord.getCompanion(),
                            addedOutGoingRecord.getCompanionPhone(),addedOutGoingRecord.getApprovalStatus(),addedOutGoingRecord.getApprover(),
                            "","", addedOutGoingRecord.getRemarks());
                    //将上面的go_out_list类添加进GoOutLists
                    goOutLists.add(goOutList);
                }else {
                    String[] actualTimeParts = sdf.format(addedOutGoingRecord.getReturnTimeActual()).split(" ");
                    String[] approvalTimeParts = sdf.format(addedOutGoingRecord.getApprovalTime()).split(" ");
                    GoOutList goOutList = new GoOutList(addedOutGoingRecord.getId(), customer.getName(), customer.getSex(),
                            customer.getAge(), customer.getArchivesid(), addedOutGoingRecord.getReason(), goOutTimeParts[0], goOutTimeParts[1],
                            forecastTimeParts[0], forecastTimeParts[1], actualTimeParts[0], actualTimeParts[1], addedOutGoingRecord.getCompanion(),
                            addedOutGoingRecord.getCompanionPhone(), addedOutGoingRecord.getApprovalStatus(), addedOutGoingRecord.getApprover(),
                            approvalTimeParts[0], approvalTimeParts[1], addedOutGoingRecord.getRemarks());
                    //将上面的go_out_list类添加进GoOutLists
                    goOutLists.add(goOutList);
                }


            }
            int total_page ;
            int offset = outgoingRecords.size()%max;
            if(offset == 0) {
                total_page = goOutLists.size() / max ;//总页数
            }else {
                total_page = goOutLists.size() / max + 1;
            }
            if(Integer.parseInt(page)>total_page){
                return new JsonResult(false,"查询页数超过总页数").toString();
            }else {

                //删除其他元素，使得outGoingRecords里面只有所需要的那一页的元素
                //删除这一页前面的
                for (int i = 0; i < (Integer.parseInt(page) - 1) * max; i++) {
                    goOutLists.remove(i--);
                }
                //取出这一页的元素
                if(Integer.parseInt(page)<total_page){
                    goOutListReturn.setTotal_page(total_page);
                    for (int i=0;i<10;i++){
                        goOutListReturn.setTotal_page(total_page);
                        goOutListReturn.addData(goOutLists.get(i));
                    }
                }else {//page==total_page
                    for (int i=0;i<(offset+9)%10+1;i++){
                        goOutListReturn.setTotal_page(total_page);
                        goOutListReturn.addData(goOutLists.get(i));
                    }
                }
            }
                return new JsonResult(true,goOutListReturn).toString();
        }
    }


    //------------------------------------------外出审核----------------------------------------------

    //获得所有申请
    @RequestMapping(value ="approval_page/getApproval",method = RequestMethod.POST)
    public @ResponseBody String getApproval(String page) {
        int max = 10;
        ArrayList<OutgoingRecord> outgoingRecords = outgoingRecordMapper.selectRecordByApprovalStatus("待审批");
        ArrayList<ApprovalGoOutRecord> approvalGoOutRecords = new ArrayList<>();
        if(outgoingRecords.size() == 0){
            return new JsonResult(false,"没有待审核信息").toString();
        }else {
            //outgoingRecords不为0，有待审核的信息
            for (int i=0;i<outgoingRecords.size();i++){
                OutgoingRecord outgoingRecord = outgoingRecords.get(i);
                Customer customer = customerMapper.selectByPrimaryKey(outgoingRecord.getCustomerId());
                String[] goOutTimeParts = sdf.format(outgoingRecord.getOutTime()).split(" ");
                String[] forecastTimeParts = sdf.format(outgoingRecord.getReturnTimeForecast()).split(" ");
                if(customer != null){
                    approvalGoOutRecords.add(new ApprovalGoOutRecord(outgoingRecord.getId(),customer.getName(),customer.getIdNumber()
                    ,outgoingRecord.getReason(),goOutTimeParts[0],goOutTimeParts[1],forecastTimeParts[0],forecastTimeParts[1],
                            outgoingRecord.getCompanion(),outgoingRecord.getCompanionRelation(),outgoingRecord.getCompanionPhone(),
                            outgoingRecord.getRemarks(),outgoingRecord.getApprovalStatus()));
                }
            }
            //现在approvalGoOutRecords里面已经有所有待审核的数据了
            //开始分页
            int offset = approvalGoOutRecords.size()%max;//最后一页的偏移
            int totalPage;
            if(offset == 0) {
                totalPage = approvalGoOutRecords.size() / max ;//总页数
            }else {
                totalPage = approvalGoOutRecords.size() / max + 1;
            }
            if(Integer.parseInt(page) > totalPage){
                return new JsonResult(false,"请求页数超过总页数").toString();
            } else if (Integer.parseInt(page)<=0) {
                return new JsonResult(false,"请求页数必须大于0").toString();
            } else {
                //清除approvalGoOutRecords里，前max*(page-1)个元素
                if (max * (Integer.parseInt(page) - 1) > 0) {
                    approvalGoOutRecords.subList(0, max * (Integer.parseInt(page) - 1)).clear();
                }
                //将这一页的元素放入最后的包装类里面
                ApprovalGoOutRecordReturn approvalGoOutRecordReturn = new ApprovalGoOutRecordReturn();
                approvalGoOutRecordReturn.setTotalPage(totalPage);
                if (Integer.parseInt(page) == totalPage) {
                    //把现在approvalGoOutRecords里的前offset个加进approvalGoOutRecordReturn里面
                    for (int i=0;i<(offset+max-1)%max+1;i++){
                        approvalGoOutRecordReturn.addData(approvalGoOutRecords.get(i));
                    }
                }else {//请求页数小于总页数

                    //把现在approvalGoOutRecords里的前max个加进approvalGoOutRecordReturn里面
                    for (int i = 0; i < max; i++) {
                        approvalGoOutRecordReturn.addData(approvalGoOutRecords.get(i));
                    }
                }
                return new JsonResult(true,approvalGoOutRecordReturn).toString();
            }
        }
    }

    //通过申请
    @RequestMapping("approval_page/approvalYes")
    public @ResponseBody String approvalYes(String uname,String id) throws ParseException {
        OutgoingRecord outgoingRecord = outgoingRecordMapper.selectByPrimaryKey(Integer.valueOf(id));
        if(outgoingRecord == null){
            return new JsonResult(false,"该id的外出单不存在").toString();
        }else {
            outgoingRecord.setApprovalStatus("审批通过");
            outgoingRecord.setApprover(uname);
            outgoingRecord.setApprovalTime(sdf.parse(sdf.format(new Date())));
            outgoingRecordMapper.updateByPrimaryKeySelective(outgoingRecord);
            return new JsonResult(true,null).toString();
        }
    }


    //不通过申请
    @RequestMapping("approval_page/approvalNo")
    public @ResponseBody String approvalNo(String uname,String id) throws ParseException {
        OutgoingRecord outgoingRecord = outgoingRecordMapper.selectByPrimaryKey(Integer.valueOf(id));
        if(outgoingRecord == null){
            return new JsonResult(false,"该id的外出单不存在").toString();
        }else {
            outgoingRecord.setApprovalStatus("未通过");
            outgoingRecord.setApprover(uname);
            //outgoingRecord.setApprovalTime(sdf.parse(sdf.format(new Date())));
            outgoingRecordMapper.updateByPrimaryKeySelective(outgoingRecord);
            return new JsonResult(true,null).toString();
        }
    }

    //通过姓名搜索待审核的外出记录
    @RequestMapping(value ="approval_page/search_by_name",method = RequestMethod.POST)
    public @ResponseBody String searchApprovalRecordByName(String name,String page) {
        int max = 10;
        ArrayList<OutgoingRecord> outgoingRecords = outgoingRecordMapper.selectRecordByApprovalStatusAndName(name,"待审批");
        ArrayList<ApprovalGoOutRecord> approvalGoOutRecords = new ArrayList<>();
        if(outgoingRecords.size() == 0){
            return new JsonResult(false,"没有待审核信息").toString();
        }else {
            //outgoingRecords不为0，有待审核的信息
            for (int i=0;i<outgoingRecords.size();i++){
                OutgoingRecord outgoingRecord = outgoingRecords.get(i);
                Customer customer = customerMapper.selectByPrimaryKey1(outgoingRecord.getCustomerId());
                String[] goOutTimeParts = sdf.format(outgoingRecord.getOutTime()).split(" ");
                String[] forecastTimeParts = sdf.format(outgoingRecord.getReturnTimeForecast()).split(" ");
                if(customer != null){
                    approvalGoOutRecords.add(new ApprovalGoOutRecord(outgoingRecord.getId(),customer.getName(),customer.getIdNumber()
                            ,outgoingRecord.getReason(),goOutTimeParts[0],goOutTimeParts[1],forecastTimeParts[0],forecastTimeParts[1],
                            outgoingRecord.getCompanion(),outgoingRecord.getCompanionRelation(),outgoingRecord.getCompanionPhone(),
                            outgoingRecord.getRemarks(),outgoingRecord.getApprovalStatus()));
                }
            }
            //现在approvalGoOutRecords里面已经有所有待审核的数据了
            //开始分页
            int offset = approvalGoOutRecords.size()%max;//最后一页的偏移
            int totalPage;
            if(offset == 0) {
                totalPage = approvalGoOutRecords.size() / max ;//总页数
            }else {
                totalPage = approvalGoOutRecords.size() / max + 1;
            }
            if(Integer.parseInt(page) > totalPage){
                return new JsonResult(false,"请求页数超过总页数").toString();
            } else if (Integer.parseInt(page)<=0) {
                return new JsonResult(false,"请求页数必须大于0").toString();
            } else {
                //清除approvalGoOutRecords里，前max*(page-1)个元素
                if (max * (Integer.parseInt(page) - 1) > 0) {
                    approvalGoOutRecords.subList(0, max * (Integer.parseInt(page) - 1)).clear();
                }
                //将这一页的元素放入最后的包装类里面
                ApprovalGoOutRecordReturn approvalGoOutRecordReturn = new ApprovalGoOutRecordReturn();
                approvalGoOutRecordReturn.setTotalPage(totalPage);
                if (Integer.parseInt(page) == totalPage) {
                    //把现在approvalGoOutRecords里的前offset个加进approvalGoOutRecordReturn里面
                    for (int i=0;i<(offset+max-1)%max+1;i++){
                        approvalGoOutRecordReturn.addData(approvalGoOutRecords.get(i));
                    }
                }else {//请求页数小于总页数

                    //把现在approvalGoOutRecords里的前max个加进approvalGoOutRecordReturn里面
                    for (int i = 0; i < max; i++) {
                        approvalGoOutRecordReturn.addData(approvalGoOutRecords.get(i));
                    }
                }
                return new JsonResult(true,approvalGoOutRecordReturn).toString();
            }
        }
    }


}
